package com.mypage.view.activity.beautMore;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.cloudcc.mobile.R;
import com.mypage.utils.UtilsShowDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeObjRecordActivity extends Activity {

    private UtilsShowDialog showDialog;
    @Bind(R.id.btnCancel)
    Button btnCancel;

    @Bind(R.id.btnSave)
    Button btnSave;
    @Bind(R.id.webMview)
    WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_obj_record);
        ButterKnife.bind(this);
        showDialog = new UtilsShowDialog(this);
        initWeb();
    }

    @OnClick(R.id.btnCancel)
    public void btnCancel() {
        ChangeRecordActivity.instance.finish();
        finish();
    }

    @OnClick(R.id.btnSave)
    public void btnSave() {
        // TODO: 2017/5/9  
    }

    public void initWeb() {
        showDialog.onShowLoadingDialog("正在加载...");
        WebSettings webSettings = webview.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(true);
        //加载需要显示的网页
        webview.loadUrl("http://www.baidu.com");
        //设置Web视图
        webview.setWebViewClient(new webViewClient());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    //设置回退
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack(); //goBack()表示返回WebView的上一页面
            showDialog.onShowLoadingDialog("正在加载...");
            return true;
        }
        finish();//结束退出程序
        return false;
    }

    //Web视图
    private class webViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            showDialog.onDismissLoadingDialog();
        }
    }
}
